# -*- coding: utf-8 -*-
"""
Created on Thu Mar 14 12:12:16 2013

@author: Maxim
"""

import propeller
import airfoil

def create_prop_from_curves2():
    import dbTools
    from numpy import array
    from scipy.interpolate import interp1d
    import matplotlib.pyplot as plt
    xNew = array([0.15,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,0.95])
    
    folder = r'D:\light aircraft\V05\propeller analysis'
    chords = dbTools.read_txt_data(folder+'\\McCauley_chord.txt')
    pitch  = dbTools.read_txt_data(folder+'\\McCauley_blade_angle.txt')
    thickness = dbTools.read_txt_data(folder + '\\McCauley_thickness.txt')
    
    chords[:,1] = chords[:,1]*0.0254
    thickness[:,1] = thickness[:,1]*0.0254
    
    chordInterp = interp1d(chords[:,0],chords[:,1],'cubic')
    thicknessInterp = interp1d(thickness[:,0],thickness[:,1],'cubic')
    pitchInterp = interp1d(pitch[:,0],pitch[:,1],'cubic')
    
    chordsNew = chordInterp(xNew)
    thicknessNew = thicknessInterp(xNew) / chordsNew
    pitchNew = pitchInterp(xNew)
    prop = propeller.propeller()
    afList = list()
    for i in range(len(xNew)):
        af = airfoil.Airfoil()
        af.read_txt('RAF6.txt',True,1)
        af.analyze_geometry()
        af.scale_thickness(thicknessNew[i])
        af.build_aero_table(alphaSeq=[-90,90,2.0])
        af.redim(75,True)
        afList.append(af)

    prop.create_prop('McCauley',1.9,0.285,3,xNew,chordsNew,pitchNew,afList,[15.,45.])
    prop.write_xls()

def analyze():
    prop = propeller.propeller()
    prop.read_xls('McCauley')
    prop.test_prop()

def read_data():
    import dbTools
    dbname = r'D:\light aircraft\V05\propeller analysis\elprop_results\prop_results.xls'
    sheetname = ['15deg_2000rpm','25deg_2000rpm','35deg_2000rpm']
    wb = dbTools.loadDB(dbname)
    J = list()
    effy = list()
    for name in sheetname:
        sh = dbTools.readDB(wb.selectByName(name))
        J.append(sh.readCol(0,3,30))
        effy.append(sh.readCol(0,11,30))
    return J,effy

def write_txt_table(path,header,tableHeader,*args):
    fid = open(path,'wt')
    fid.write('%s\n'%header)
    for col in tableHeader:
        fid.write('%s\t'%col)
    fid.write('\n')
    n = len(args[0])
    for i in range(n):
        for col in args:
            print col[i]
            fid.write('%.6f\t'%col[i])
        fid.write('\n')
    fid.close()

def compare_propellers2():
    from numpy import linspace, zeros
    import matplotlib.pyplot as plt
    from copy import copy

    names = ['NACA5868','neuform']
    names2 = ['MT173','neuform']
    N = 2000.0
    density = 1.2255
    V = linspace(5.,100.,25)
    beta = [15.0,25.0,35.0]
    marker = ['o-','*-','s-','v-','8-']
    color = ['b','r','m']
    J      =list()
    effy   =list()
    legend =list()

    Jxls,effyXls = read_data()

    plt.figure(13)
    plt.hold(True)
    plt.grid(True)
    for k,name in enumerate(names):
        prop = propeller.propeller()
        prop.read_xls(name)
        J1 = zeros([len(beta),len(V)])
        effy1 = zeros([len(beta),len(V)])
        print prop.sigma*prop.diameter / prop.numBlades, prop.name
        for i,b in enumerate(beta):
            for j,v in enumerate(V):
                data = prop.analyze_prop(b,N,v,density)
                J1[i,j] = data[2]
                effy1[i,j] = data[5]
            legend.append('{0:8}- {1:1.0f}blade:{2:2.0f}deg'.format(names2[k],prop.numBlades,b))
        effy.append(copy(effy1))
        J.append(copy(J1))
        for ii,b in enumerate(beta):
            plt.plot(J[k][ii,:],effy[k][ii,:],color[k]+marker[ii])
    plt.axis([0,J[0][0,-1],0,1.0])
    legend.append('{0:8}- {1:1.0f}blade:{2:2.0f}deg'.format('Elprop',3,beta[0]))
    legend.append('{0:8}- {1:1.0f}blade:{2:2.0f}deg'.format('Elprop',3,beta[1]))
    legend.append('{0:8}- {1:1.0f}blade:{2:2.0f}deg'.format('Elprop',3,beta[2]))
    plt.plot(Jxls[0],effyXls[0]/100,'go-')
    plt.plot(Jxls[1],effyXls[1]/100,'g*-')
    plt.plot(Jxls[2],effyXls[2]/100,'gs-')
    plt.legend(legend,4)
    plt.xlabel('advance ratio')
    plt.ylabel('efficiency')
    plt.title('Propeller efficiency comparison')
    plt.show()
    
    tableHeader = ['J','V','efficiency']
    write_txt_table('2blade - 15deg.txt',legend[0],tableHeader,J[0][0],V,effy[0][0])
    write_txt_table('3blade - 15deg.txt',legend[3],tableHeader,J[1][0],V,effy[1][0])
    write_txt_table('2blade - 25deg.txt',legend[1],tableHeader,J[0][1],V,effy[0][1])
    write_txt_table('3blade - 25deg.txt',legend[4],tableHeader,J[1][1],V,effy[1][1])

def compare():
    from numpy import linspace, zeros
    import matplotlib.pyplot as plt
    from copy import copy
    
    propellers = ['NACA5868','neuform']
    N = 2000.0
    density = 1.2255
    beta = [15.0,25,35]
    
    

if __name__=="__main__":
    compare_propellers2()